﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace VMC.Domain.Service
{
    public class ValidationState
    {
        private bool _isValid;
        private Dictionary<string, string> _errors;
        public ValidationState()
        {
            _isValid = true;
            _errors = new Dictionary<string, string>();
        }

        public void AddError(string MessageLabel, string Message)
        {
            _errors.Add(MessageLabel, Message);
        }

        public void CheckForEmptyString(string UserInput, int AllowedMaxLength, string MessageLabel)
        {
            if (string.IsNullOrEmpty(UserInput) || UserInput.Trim().Length == 0)
            {
                AddError(MessageLabel, MessageLabel + " is required and can not be empty.");
            }
            else if (UserInput.Trim().Length > AllowedMaxLength)
            {
                AddError(MessageLabel, MessageLabel + " is limited to " + AllowedMaxLength.ToString() + "  characters.");
            }
        }
        public void CheckForStringLength(string UserInput, int AllowedMaxLength, string MessageLabel)
        {
            if (!string.IsNullOrEmpty(UserInput))
            {
                if (UserInput.Trim().Length > AllowedMaxLength)
                {
                    AddError(MessageLabel, MessageLabel + " is limited to " + AllowedMaxLength.ToString() + "  characters.");
                }
            }
        }
        public void CheckForEmptyString(string UserInput, string MessageLabel)
        {
            CheckForEmptyString(UserInput, int.MaxValue, MessageLabel);
        }
        public void CheckForEmailAddress(string Email, string MessageLabel)
        {
            Regex regex = new Regex(@"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$", RegexOptions.IgnorePatternWhitespace);
            if (!regex.IsMatch(Email))
                AddError(MessageLabel, MessageLabel + " is not a valid email address.");
        }
        public void CheckForVariableName(string VariableName, string MessageLabel)
        {
            Regex regex = new Regex("^[a-zA-Z_]*$");
            if (!regex.IsMatch(VariableName))
                AddError(MessageLabel, VariableName + " is not a valid variable name. It can only contain a-z, A-Z and _.");
        }
        public void CheckForPositiveID(int ID, string MessageLabel)
        {
            if (ID <= 0)
                AddError(MessageLabel, "Invalid value for" + MessageLabel);
        }
        public void ClearValidation()
        {
            _errors.Clear();

            _isValid = true;
        }

        public bool IsValid
        {
            get
            {
                if (_errors.Count > 0) _isValid = false;
                else _isValid = true;
                return _isValid;
            }
        }

        public Dictionary<string, string> ValidationErrors
        {
            get { return _errors; }
        }
    }
}
